package com.joysuch.wwyt.workflow.repository;

import com.joysuch.wwyt.risk.enums.EvaluationTypes;
import com.joysuch.wwyt.workflow.entity.WorkFlowTaskAssignHistory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface WorkFlowTaskAssignHistoryDao extends JpaRepository<WorkFlowTaskAssignHistory, Long> {

    List<WorkFlowTaskAssignHistory> findByTaskIdAndTaskName(Long taskId, String taskName);

    List<WorkFlowTaskAssignHistory> findByTaskCodeOrderById(String taskCode);

    @Query(value = " SELECT " +
            "* " +
            "FROM " +
            "WWYT_WORKFLOW_TASK_ASSIGN_HISTORY " +
            "WHERE delete_flag = '0' " +
            "AND task_code = ?1 " +
            "ORDER BY id ASC " +
            "LIMIT 1 " , nativeQuery = true)
    WorkFlowTaskAssignHistory findFirstByTaskCodeOrderById (String taskCode);

    @Query(value = " SELECT " +
            "* " +
            "FROM " +
            "WWYT_WORKFLOW_TASK_ASSIGN_HISTORY " +
            "WHERE delete_flag = '0' " +
            "AND task_code = ?1 " +
            "AND next_task_name is null " +
            "ORDER BY id DESC " +
            "LIMIT 1 " , nativeQuery = true)
    WorkFlowTaskAssignHistory findLastByTaskCodeOrderById(String taskCode);

    
    @Query(value = "select * from WWYT_WORKFLOW_TASK_ASSIGN_HISTORY where delete_flag = '0' AND task_code = ?1 and next_task_name = ?2 order by id asc limit 1", nativeQuery = true)
    WorkFlowTaskAssignHistory findOneByTaskCodeAndNextTaskNameOrderByIdAsc(String taskCode ,String taskName);
    
    @Query(value = "select * from WWYT_WORKFLOW_TASK_ASSIGN_HISTORY where delete_flag = '0' AND task_code = ?1 and task_name = ?2 order by id desc limit 1", nativeQuery = true)
    WorkFlowTaskAssignHistory findOneByTaskCodeAndTaskNameOrderByIdDesc(String taskCode ,String taskName);
}
